﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EasySamba.Controllers
{
    public class Comn
    {
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <returns>结果</returns>
        static public Result<IEnumerable<string>> LoadConfigContent()
        {
            var configFullName = Startup.Configuration["ConfigName"];

            var success = false;
            var configContent = Enumerable.Empty<string>();
            var massage = string.Empty;
            try
            {
                configContent = System.IO.File.ReadAllLines(configFullName, System.Text.Encoding.UTF8);
                success = true;
            }
            catch (Exception e)
            {
                massage = $"加载配置文件失败！原因：{e.Message}";
            }

            return new Result<IEnumerable<string>>(success, configContent, massage);
        }

        /// <summary>
        /// 保存配置文件
        /// </summary>
        /// <param name="password">密码</param>
        /// <param name="configContent">文件内容</param>
        /// <returns></returns>
        static public Result SaveConfigContent(string password, string configContent)
        {
            var configFullName = Startup.Configuration["ConfigName"];

            var success = false;
            var result = string.Empty;

            var adminPassword = Startup.Configuration["Password"];
            if (password != adminPassword)
            {
                return new Result(false, "密码错误！");
            }

            try
            {
                System.IO.File.Delete(configFullName);
                System.IO.File.WriteAllText(configFullName, configContent, System.Text.Encoding.UTF8);

//#if !DEBUG
//                System.Diagnostics.Process.Start("systemctl", " restart kestrel-EasySamba.service");
//#endif

                success = true;
            }
            catch (Exception e)
            {
                configContent = $"保存配置文件失败！原因：{e.Message}";
            }

            return new Result(success, configContent);
        }

        /// <summary>
        /// 加载配置文件为结构化数据
        /// </summary>
        /// <returns>FullConfig</returns>
        static public Result<FullConfig> LoadFullConfig()
        {
            var result = new Result<FullConfig>(false, null, string.Empty);

            var contentResult = LoadConfigContent();
            if (contentResult.Success)
            {
                FullConfig fullConfig = new FullConfig();
                fullConfig.FromTextLines(contentResult.Data);
                result.Success = true;
                result.Data = fullConfig;
            }
            else
            {
                result.Massage = contentResult.Massage;
            }

            return result;
        }

        /// <summary>
        /// 保存配置文件
        /// </summary>
        /// <param name="password">密码</param>
        /// <param name="fullConfig">结构化文件数据</param>
        /// <returns></returns>
        static public Result SaveFullConfig(string password, FullConfig fullConfig)
        {
            var textLines = fullConfig.ToTextLines();
            var configContent = string.Join(Environment.NewLine, textLines);
            var result = SaveConfigContent(password, configContent);
            return result;
        }

        static public Tuple<bool, string, string> SplitKeyValue(string line)
        {
            var keyValue = line.Split('=', StringSplitOptions.RemoveEmptyEntries);
            if (keyValue.Length == 2)
            {
                return new Tuple<bool, string, string>(true, keyValue[0].Trim(), keyValue[1].Trim());
            }
            else
            {
                return new Tuple<bool, string, string>(false, null, null);
            }
        }

        static public string ToLine(string key, string value)
        {
            return $"{key} = {value}";
        }
    }
}
